package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Slf4j
@RestController
@Api(tags = "APP菜品分类管理")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    // 分类列表
    @ApiOperation(value = "列表查询菜品分类")
    @Cacheable(value = "appDish")
    @GetMapping("/category/list")
    public ResultInfo findAll() {

//        List<Category> list = null;
//
//        // 先查询缓存
//        String categoryKey = "category_list";
//
//        if (redisTemplate.hasKey(categoryKey)) {
//            list = (List<Category>) redisTemplate.opsForValue().get(categoryKey);
//            log.info("查询redis缓存");
//        } else {
//            // 调用service查询MySQL
//            list = categoryService.findAllCategory();
//            log.info("查询MySQL数据库");
//            // 同步到redis中
//            redisTemplate.opsForValue().set(categoryKey, list);
//        }

        List<Category> list = categoryService.findAllCategory();

        // 返回结果
        return ResultInfo.success(list);
    }



}